package com.ale.oxetool.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Created by wgs on 2016/3/26.
 */

@Controller
public class IndexController {

    private static Logger log = LoggerFactory.getLogger(PhoneController.class);
    @Autowired
    private MessageSource messageSource;
    @Autowired
    private ObjectMapper objectMapper;

    @Value("${filterFeatureEnabled:false}")
    private boolean filterFeatureEnabled;

    @Value("${phoneGroupFile:}")
    private String phoneGroupFile;

    @RequestMapping("/")
    public String showIndex(Model model, @Value("${currentsite}") String currentSite) throws IOException {
        Locale locale = LocaleContextHolder.getLocale();
        Map<String, Object> clientMap = new HashMap<>();
        clientMap.put("title", messageSource.getMessage("title",
                new Object[]{messageSource.getMessage(currentSite, null, locale)}, locale));
        clientMap.put("locale", locale.toLanguageTag().toLowerCase());
        clientMap.put("filterFeatureEnabled", filterFeatureEnabled);
        clientMap.put("groupFeatureEnabled", !phoneGroupFile.isEmpty());

        model.addAttribute("globalObj", objectMapper.writeValueAsString(clientMap));
        return "index";
    }

}
